package cn.pn.project.service.impl;


import cn.pn.project.mapper.base.CityPositioningDao;
import cn.pn.project.pojo.base.City;
import cn.pn.project.service.CityPositioningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cityPositioning")
public class CityPositioningServiceImpl implements CityPositioningService {

    @Autowired
    private CityPositioningDao cityPositioningDao;

    @Override
    @PostMapping("/p/findCity")
    public City findCityBycityName(@RequestParam("cityName") String cityName) throws Exception {
        City city=cityPositioningDao.findCityBycityName(cityName);

        return city;
    }


    @Override
    @PostMapping("/p/getAllInfo")
    public List<City> listCity() throws Exception {
        List<City> ALLlist=cityPositioningDao.listCity();
        List<City> resultList=new ArrayList<City>();
        for(int i=0;i < ALLlist.size();i++){
            City city= ALLlist.get(i);
            if(city.getFatherId().equals(0)){
               continue;
            }
            boolean result=false;
            for(int j=0;j<ALLlist.size();j++){
                City c=ALLlist.get(j);
                if(city.getFatherId().equals(c.getCityId()) && (c.getFatherId().equals(0)||c.getFatherId().equals(c.getCityId()))){
                    result=true;
                    break;
                }
            }
            if(result)
                resultList.add(city);
        }
        System.out.println(resultList.size());
        return resultList;
    }

    @Override
    @PostMapping("/p/findAll")
    public List<City> findAll() throws Exception {
        return cityPositioningDao.listCity();
    }

    @Override
    @PostMapping("/p/getInfoTopTen")
    public List<City> listCityTopTen() throws Exception {
        List<City> city=cityPositioningDao.listCityTopTen();
        return city;
    }
}
